-module(ehttp_handler).

-export([init/2]).

-include("ehttpd.hrl").


init(Req, Opts) ->
    try
        ehttp_dispatcher:start(Req, Opts)
    catch
        Error:Reason:StackTrace ->
            Path = binary_to_list(cowboy_req:path(Req)),
            Method = cowboy_req:method(Req),

            ?EH_ERR("Path:~s,Method:~s,Error:~p:Reason:~p,StackTrace:~n~p", [Path, Method, Error, Reason, StackTrace]),

            Req2 = cowboy_req:reply(500, [{<<"content-type">>, <<"text/plain;charset=utf-8">>}], <<"request failed, sorry\n">>, Req),
            {ok, Req2, Opts}
    end.
